/**
 * 9-1
 * 9.2.c
 * */
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
void main(int argc, char **argv)
{
  char ipbuf[20];     // save ip address
  unsigned int ipint; // ip地址的整数形式
  struct in_addr addrstr;
  char *ch = NULL;
  while (1)
  {
    printf("please input ip address\n");
    fgets(ipbuf, 20, stdin);
    ipbuf[strlen(ipbuf) - 1] = '\0'; // 去掉fgets输入的\n
    if (!strcmp(ipbuf, "exit")) break;
    printf("The binary of ip address %s is %x\n", ipbuf, inet_addr(ipbuf));
    printf("The host byte of binary ip address is %x\n", inet_network(ipbuf));
    inet_aton(ipbuf, &addrstr);
    ch = (char *)&addrstr;
    printf("The inet_aton result is %x\n", addrstr.s_addr);
    printf("The network byte of ip address is %x.%x.%x.%x\n", (*ch) & 0xff, *(ch + 1) & 0xff, *(ch + 2), *(ch + 3));
    printf("The host id of the ip address is %x\n", inet_lnaof(addrstr));
    printf("The network id of the ip address is %x\n", inet_netof(addrstr));
    addrstr.s_addr++;
    ch = (char *)&addrstr;
    printf("The new ip address is %s\n", inet_ntoa(addrstr));
    printf("The inet_aton result of new ip address is %x\n", addrstr.s_addr);
    printf("The network byte of new ip address is %x.%x.%x.%x\n", (*ch) & 0xff, *(ch + 1) & 0xff, *(ch + 2), *(ch + 3));
    printf("The host id of the new ip address is %x\n", inet_lnaof(addrstr));
    printf("The network id of new ip address is %x\n", inet_netof(addrstr));
  }
}
